//
//  MKPageViewController.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKPageViewController.h"
#import "MKBaseHeader.h"
@interface MKPageViewController ()<sendSeq_no>
@property (nonatomic, retain) MKComicModel *comicModel;
@property (nonatomic, strong) NSMutableArray *pageArray;
@end

@implementation MKPageViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 24, 100, 30)];
    
    [self.navBarView addSubview:titleLabel];
    
    [titleLabel setText:@"全部章节"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(10, 24, 30, 30)];
    //[backButton setBackgroundColor:[UIColor redColor]];
    [self.navBarView addSubview:backButton];
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];

    self.automaticallyAdjustsScrollViewInsets = YES;
    

    self.MainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    _MainScrollView.showsVerticalScrollIndicator = NO;
    [_MainScrollView setBackgroundColor:[UIColor whiteColor]];
    
    
    [self.view addSubview:_MainScrollView];
    _pageArray = [[NSMutableArray alloc] init];
    
    [AFNETWORK getDataWithNoCacheString:self.str1 block:^(id block) {

        
        self.comic_id = [[[block objectForKey:@"data"] objectForKey:@"comic"]objectForKey:@"comic_id" ];
        //        self.vip_state = [[[block objectForKey:@"data"] objectForKey:@"comic"]objectForKey:@"vip_state" ];
        
        _comicModel = [[MKComicModel alloc] initWithDictionary:[[block objectForKey:@"data"] objectForKey:@"comic"]];
        
        
        self.chapterModelArray = [[NSMutableArray alloc] init];
        
        
        for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"chapters"]) {
            
            MKChaptersModel *chaptersModel = [[MKChaptersModel alloc] initWithDictionary:dic];
            
            if ([_comic_id isEqualToString:@"12769"] && chaptersModel.seq_no.integerValue > 58) {
                chaptersModel.seq_no = [NSString stringWithFormat:@"%ld", chaptersModel.seq_no.integerValue - 1];
            }
            
            [self.chapterModelArray addObject:chaptersModel];
        }
        
#pragma mark - 章节button
        
        for (int i = 0; i < self.chapterModelArray.count; i++) {
            
            MKChaptersModel *model = [[MKChaptersModel alloc] init];
            model = _chapterModelArray[i];
            
            self.pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.pageButton setFrame:CGRectMake(10 + ((i % 4)* (self.view.frame.size.width - 35) / 4  + 15), 15 + 35 * (i / 4) , (self.view.frame.size.width - 35) / 5, 30)];
            [_pageButton setTitle:model.seq_no forState:UIControlStateNormal];
            
            self.pageButton.layer.masksToBounds = YES;
            [self.pageButton.layer setCornerRadius:5];
            self.pageButton.layer.borderWidth = .5;
            self.pageButton.layer.borderColor = [UIColor grayColor].CGColor;
            [self.pageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.pageButton.titleLabel.font = [UIFont systemFontOfSize: 15];
            [self.MainScrollView addSubview:self.pageButton];
            
            // 设置点击进入对应的漫画开始阅读
            [self.pageButton addTarget:self action:@selector(ReadClick:) forControlEvents:UIControlEventTouchUpInside];
            [_pageArray addObject:self.pageButton];
            NSString *string = [self.pageNumber stringByReplacingOccurrencesOfString:@"第" withString:@""];
            NSString *newString = [string stringByReplacingOccurrencesOfString:@"话" withString:@""];
            
            if ([_pageButton.titleLabel.text isEqualToString:newString]) {
                [_pageButton setBackgroundColor:[UIColor redColor]];
            } else {
                [_pageButton setBackgroundColor:[UIColor whiteColor]];
            }
            
        }
        
        [_MainScrollView setContentSize:CGSizeMake(0, self.pageButton.frame.origin.y + 40)];
        
    }];

}



#pragma mark - 在navigation上铺个返回键
-(void)backClick
{
    
    //从哪过来的 回哪去
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)sendSeq_no:(NSString *)string{
    
    
    NSString *newString = [string stringByReplacingOccurrencesOfString:@"第" withString:@""];
    NSString *newsetString = [newString stringByReplacingOccurrencesOfString:@"话" withString:@""];
    
    for (UIButton *button in _pageArray) {
        if ([button.titleLabel.text isEqualToString:newsetString]) {
            [button setBackgroundColor:[UIColor redColor]];
        } else {
            
            [button setBackgroundColor:[UIColor whiteColor]];
            
        }
    }
    
    [self.sendPageDelegate sendPage:string];
    
}


#pragma mark - 点击进入阅读漫画
-(void)ReadClick:(UIButton *)sender
{

    MKBrowseCartoonViewController *browse = [[MKBrowseCartoonViewController alloc] init];
    browse.delegate = self;
    for (MKChaptersModel *model in _chapterModelArray ) {
        if ([model.seq_no isEqualToString:sender.titleLabel.text]) {
            browse.chapterModel = model;
            browse.seq_no = model.seq_no.integerValue;
            browse.comicModel = _comicModel;
            browse.chapterModelArray = _chapterModelArray;
        }
    }
    if ([self.vip_state isEqualToString:@"1"]) {
        
        [self.navigationController pushViewController:browse animated:NO];
    }else{
        UIAlertView *AlertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"本漫画是付费漫画下次版本更新会提供观看" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [AlertView show];

    }
    //[self.delegate stringByString:sender.titleLabel.text];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
